# This is the job submission script to resample the AWAP ASCII files of daily max and min temp
# To 250 m resolution for the WT plus Buffer regions

# Establish in directory

LT.dir = '/home1/99/jc152199/GRASS/LT/fcell/'
out.dir = '/home1/99/jc152199/GRASS/output'
setwd(lt.dir)

#### Make a list of all Linke Turbidity Files
#### Use the file name to create a list of numbers representing the day of year on which the LT file is based
	
LTfiles = list.files(LT.dir,pattern='lt')
lt.days = as.numeric(substr(LTfiles,4,6))


# Establish a temporary directory to write out the .sh files to
# This keeps your workspace more orderly and should the script fuck up all error logs will be stored in one location

tmp.pbs = '/home1/99/jc152199/tmp.pbs/'
setwd(tmp.pbs)

# Begin a loop to create 2 sh files for each DOY
# The first sh file will create the GRASS commands
# The second sh file will load the commands into GRASS
# Finally use qsub to submit commands

for(dayx in c(1:365)) 

	{
	
	
	# Determine the difference between dayx and the possible LT files
	day.diff = abs(lt.days - dayx)
	# Calculate the minimum of these differences, this is the number of the LT file to use
	min.diff = min(day.diff)
	# Select the appropriate LT file
	t.LT = sprintf('%03i',lt.days[which(day.diff %in% min.diff)])
	
	#Create a three digit day to use
	
	t.day = sprintf('%03i',dayx)
	
	# Create file name for GRASS command .sh file
	
	tname = paste('day',t.day,'.sh',sep='')
	
	# Create the file with GRASS commands
	
	zz = file(tname,'w')
	
		cat("r.sun -s --overwrite elevin=dem250bigbuffer@PERMANENT aspin=aspect250bigbuffer@PERMANENT slopein=slope250bigbuffer@PERMANENT linkein=lt_",t.LT,"@LT insol_time=insoltime_",t.day,"@output glob_rad=globrad_",t.day,"@output day=",dayx,"\n",sep='',file=zz)
		cat("r.out.arc input=insoltime_",t.day,"@output output=/home1/99/jc152199/GRASS/output/insoltime_",t.day,".asc dp=1\n",sep='',file=zz)
		cat("r.out.arc input=globrad_",t.day,"@output output=/home1/99/jc152199/GRASS/output/totalrad_",t.day,".asc dp=0\n",sep='',file=zz)
	
	close(zz)

	# Create the file to submit to the HPC
	
	ttname = paste('2submit',t.day,'.sh',sep='')
	
	yy = file(ttname,'w')
	
		cat('##################################\n',file=yy)
		cat('#!/bin/sh\n',file=yy)
		cat('cd $PBS_O_WORKDIR\n',file=yy)
		cat('chmod u+x /home1/99/jc152199/tmp.pbs/',tname,'\n',sep='',file=yy)
		cat('export GRASS_BATCH_JOB=/home1/99/jc152199/tmp.pbs/',tname,'\n',sep='',file=yy)
		cat('grass64 /home1/99/jc152199/GRASS/output\n',sep='',file=yy)
		cat('unset GRASS_BATCH_JOB\n',sep='',file=yy)
		cat('#################################',sep='',file=yy)
		
	close(yy)	
	
	# The following line submits the job to the HPC, leave it commented out until you're ready to RUN all the jobs\
	# Always run the script first without removing the comment, this will still generate the .sh files for you to check before running
	# Once you're happy with that then submit a single .sh file and make sure it runs through to completion before batching out the rest with the following line
	system(paste('qsub -l nodes=1:ppn=8 ',ttname,sep=''))

	}

# Done